package com.fang.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Bernie_fang
 * @Description:
 * @create 2025/1/30 16:48
 **/
public class DateUtils  {

    public static final String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_DATE_HOUR = "yyyy-MM-dd HH";
    public static final String FORMAT_DATE = "yyyy-MM-dd";



    /**
     * Description: 获取yyyy-MM-dd格式的日期字符串
     * @Author: Bernie_fang
     * @Since: 2025/1/30 16:48

     * @return: java.lang.String. if date is null, return null
     **/
    public static String getDateString() {
        Date date = new Date();
        return getDateString(date);
    }

    /**
     * Description: 获取yyyy-MM-dd格式的日期字符串
     * @Author: Bernie_fang
     * @Since: 2025/1/30 16:54
     * @param date:
     * @return: java.lang.String. if date is null, return null
     **/
    public static String getDateString(Date date) {
        if (isEmpty(date)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE);
        return sdf.format(date);
    }

    /**
     * Description: 获取yyyy-MM-dd HH:mm:ss格式的日期时间字符串
     * @Author: Bernie_fang
     * @Since: 2025/1/30 16:55
     * @return: java.lang.String. if date is null, return null
     **/
    public static String getDateTimeString() {
        Date date = new Date();
        return getDateTimeString(date);
    }

    /**
     * Description: 获取yyyy-MM-dd HH:mm:ss格式的日期时间字符串
     * @Author: Bernie_fang
     * @Since: 2025/1/30 16:55
     * @param date:
     * @return: java.lang.String. if date is null, return null
     **/
    public static String getDateTimeString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE_TIME);
        return sdf.format(date);
    }

    /**
     * Description: 将传入时间按指定格式转化为字符串
     * @Author: Bernie_fang
     * @Since: 2025/5/10 23:11
     * @param date:
     * @param format:
     * @return: java.lang.String
     **/
    public static String getDateString(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * Description: 将传入字符串按指定格式解析为时间
     * @param dateStr:
     * @param format:
     * @Author: Bernie_fang
     * @Since: 2025/5/10 23:15
     * @return: java.util.Date
     **/
    public static Date getDateDate(String dateStr, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(dateStr);
    }

    public static boolean isNotEmpty(Date date) {
        return !isEmpty(date);
    }

    public static boolean isEmpty(Date date) {
        return date == null;
    }
}
